<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/23
 * Time: 15:04
 */

namespace app\lib;



use Flc\Dysms\Client;
use think\Cache;

class SendSms
{
    protected $config = [
        'accessKeyId' => 'LTAIzd9PqJX5yHCY',
        'accessKeySecret' => '72A5vBOiRCDZrEO0ozx5rxyaCgtEbp'
    ];
    protected $hpone;
    protected $code;
    public function __construct($hpone)
    {
        $this->hpone = $hpone;
        $this->getcode();
    }

    public function toSendCode(){
        $client = new Client($this ->config);
        $sendSms = new \Flc\Dysms\Request\SendSms();
        $sendSms->setPhoneNumbers($this->hpone);
        $sendSms->setSignName('悟空维修');
        $sendSms->setTemplateCode('SMS_150500765');
        $sendSms->setTemplateParam(['code' => $this->code]);
        $sendSms->setOutId('lala');
        $re = (array)($client->execute($sendSms));
        if ($re['code'] == 'OK'){
            return true;
        }else{
            return $re;
        }
    }

    protected function getcode(){
        if ($code = Cache::get($this->hpone)){
           $this->code = $code;
        }else{
            $this->code = rand(100000, 999999);
            Cache::set($this->hpone,$this->code,time()+300);
        }
    }

    public function varify($code){
        if ($this -> code = Cache::get($this->hpone)){
            if ($code == $this->code){
                return true;
            }
        }
        return false;
    }

}